//
//  AppDelegate.swift
//  Hyy9Weibo
//
//  Created by Air on 16/3/21.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setAppearance()
        
        
        
        //TODO : 测试记载帐号
        let userAccount = HYYUserAccountModel.sharedInstance.userAccount
        print("AppDelegate userAccount : \(userAccount)")
        
        window = UIWindow(frame:  UIScreen.mainScreen().bounds)
        
        //创建tarbar
//        let tabBarVC = HYYMainViewController()
        
        //设置根控制器
        window?.rootViewController = defaultViewController()
        
        //当你一心一意开发一个界面的时候 将window?.rootViewController设置为我的控制器
//        window?.rootViewController = HYYNewFeatureViewController()
    
        window?.makeKeyAndVisible()

        
        
        return true
    }
    
    /**
     界面切换，不需人工介入
     
     - returns: 默认控制器
     */
    private func defaultViewController()->UIViewController{
        if !HYYUserAccountModel.sharedInstance.userLogin {
            //没有登陆
            return HYYMainViewController()
        }
        
        //登陆过了
        return isNewVersion() ? HYYNewFeatureViewController() : HYYWelcomeViewController()
        
    }
    
    /**
     界面切换统一调用这个方法,到时候如果界面切换出问题，直接在这个方法里打断电，方便调试
     
     - parameter controller: 要切换的控制器
     */
    func swithRootViewController(controller: UIViewController){
        window?.rootViewController = controller
    }
    //外面使用需要拿到对象在调用，比较麻烦
    class func outSwitchRootViewController(controller: UIViewController){
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.swithRootViewController(controller)
    }
    
    
    /**
     是否是新版本
     
     - returns: 是否是新版本
     */
    private func isNewVersion()->Bool{
        //1.获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        HYYPrint("version : \(version)")
        //2.获取上一次版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
//        HYYPrint("sandboxVersion : \(sandboxVersion)")
        //3.对比,不一样就是新版本
        let newVersion = version != sandboxVersion
        //4.是新版本，保存当前版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        //5.返回比较结果
        //TODO:
        return newVersion
    }
    
    private func setAppearance(){
        //设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

